package main;


import java.util.Random;
import java.util.Vector;
import main.Learnone;


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author евгений
 */
public class Learndata {

    private Vector<Learnone> data;
    private Current current;
    private Random rand;
    private int minshowcount=10;
    private double whenlearded=0.9;

    public double getWhenlearded() {
        return whenlearded;
    }

    public void setWhenlearded(double whenlearded) {
        this.whenlearded = whenlearded;
    }


    public int getMinshowcount() {
        return minshowcount;
    }

    public void setMinshowcount(int minshowcount) {
        this.minshowcount = minshowcount;
    }


    private Current getCurrent() {
        return current;
    }

    private void setCurrent(Current current) {
        this.current = current;
    }

    public Learndata() {
        data=new Vector<Learnone>();
        rand=new Random();
        current=new Current();
    }

    public void add(String word, String translate, int show, int show1, int right, int right1){
        Learnone one =new Learnone(word,translate,show,show1,right,right1);
        data.add(one);
    }

    public int count(){
        int j=0;
        for (int i=0;i<data.size();i++){
            if (!data.get(i).isShowed() && !learned(true, i))
                j++;
            if (!data.get(i).isShowed1() && !learned(false, i))
                j++;

        }
        if (j==0 && data.size()>0){
            for (int i=0;i<data.size();i++){
                data.get(i).setShowed(false);
                data.get(i).setShowed1(false);
            }
            j=data.size()*2;
        }
        return j;
    }

    private boolean learned(boolean first, int element){
        if (first && data.get(element).getShow()>getMinshowcount() && data.get(element).getRight()/data.get(element).getShow()>getWhenlearded())
            return true;
        if (!first && data.get(element).getShow1()>getMinshowcount() && data.get(element).getRight1()/data.get(element).getShow1()>getWhenlearded())
            return true;
        return false;

    }

    public String Show(){
        int next=(new Double(count()*rand.nextDouble())).intValue()+1;
        int i=0;
        int j=-1;
        boolean first=true;
        while (i<next){
            if (first)
                j++;
            if (first && !data.get(j).isShowed() && !learned(true, j)){
                i++;
            }else
                if (!first && !data.get(j).isShowed1() && !learned(false, j)){
                    i++;
                }
            first=!first;

        }
        String ret="";
        if (!first){
            data.get(j).setShowed(true);
            data.get(j).setShow(data.get(j).getShow()+1);
            setCurrent(new Current(j,true));
            ret=data.get(j).getWord();
        }else{
            data.get(j).setShowed1(true);
            data.get(j).setShow1(data.get(j).getShow1()+1);
            setCurrent(new Current(j,false));
            ret=data.get(j).getTranslate();

        }
        return ret;

    }

    public String Word(){
        if (getCurrent().isFirst()){
            return data.get(getCurrent().getCurrent()).getWord();
        }else{
            return data.get(getCurrent().getCurrent()).getTranslate();
        }

    }

    public String Translate(){
        if (!getCurrent().isFirst()){
            return data.get(getCurrent().getCurrent()).getWord();
        }else{
            return data.get(getCurrent().getCurrent()).getTranslate();
        }

    }
    public void setright(){
        if (getCurrent().isFirst()){
            data.get(getCurrent().getCurrent()).setRight(data.get(getCurrent().getCurrent()).getRight()+1);
        }else{
            data.get(getCurrent().getCurrent()).setRight1(data.get(getCurrent().getCurrent()).getRight1()+1);
        }
    }
    public int size(){
        return data.size();
    }
    public Learnone get(int i){
        return data.get(i);
    }
}
